﻿using CollegeSim.CourseScheduler.Presentation.Facade;
using System;
using System.Windows.Forms;

namespace CollegeSim.CourseScheduler.Presentation
{
    // Author: Ryan Redburn
    // Project: Course Scheduler
    // Date: 2/16/2014

    #region [ Updates ]

    // Author: Ryan Redburn
    // Date: 4/6/2014
    // Revision: Wired up delete button and added comments.

    // Author: Ryan Redburn
    // Date: 5/20/2014
    // Revisions: Added a button column to the datagridview that shows the course action menu for the row that is clicked on.

    #endregion

    public partial class FormCourseManagement : Form
    {
        #region [ Fields ]

        private readonly SchedulerFacade _facade;

        #endregion

        #region [ Constructors ]

        // Initialize form resources
        public FormCourseManagement(SchedulerFacade facade)
        {
            InitializeComponent();
            _facade = facade;
        }

        #endregion

        #region [ Events ]

        // Displays course table on load
        private void FormViewCourses_Load(object sender, EventArgs e)
        {
            try
            {
                DisplayCourseTable();
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show(ex.Message, "Network Error");
                Application.Exit();
            }
        }

        // Displays the course action menu when a user clicks on the "Select" button
        private void dgvCourses_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                // Confirm the user clicked a button cell
                if (dgvCourses.Columns[e.ColumnIndex].Name != "Course Select") return;

                // Get course ID value from the datagridview
                var courseID = dgvCourses.Rows[dgvCourses.CurrentCell.RowIndex].Cells[1].Value.ToString();

                if (courseID != "")
                {
                    using (var frmActionSelect = new FormCourseActionSelect(_facade, int.Parse(courseID)))
                    {
                        frmActionSelect.ShowDialog();
                    }
                }
                else
                {
                    MessageBox.Show("Invalid course selection.", "Invalid Selection");
                }
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show(ex.Message, "Network Error");
                Application.Exit();
            }
        }

        // Starts creation process
        private void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                using (var frmCreate = new FormCourseCreate(_facade, false))
                {
                    frmCreate.ShowDialog();
                }

                DisplayCourseTable();
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show(ex.Message, "Network Error");
                Application.Exit();
            }
        }

        // Starts update process
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                using (var frmUpdate = new FormCourseCreate(_facade, true))
                {
                    frmUpdate.ShowDialog();
                }

                DisplayCourseTable();
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show(ex.Message, "Network Error");
                Application.Exit();
            }
        }

        // Starts deletion process
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                using (var frmDelete = new FormCourseDelete(_facade))
                {
                    frmDelete.ShowDialog();
                }

                DisplayCourseTable();
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show(ex.Message, "Network Error");
                Application.Exit();
            }
        }

        // Refreshes the course table
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                DisplayCourseTable();
            }
            catch (InvalidOperationException x1)
            {
                MessageBox.Show(x1.Message, "Network Error");
                Application.Exit();
            }
        }

        // Closes the form
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion

        #region [ Internal Methods ]

        /// <summary>
        /// Creates and displays the course table in the course datagridview.
        /// </summary>
        private void DisplayCourseTable()
        {
            dgvCourses.Columns.Clear();
            dgvCourses.Rows.Clear();
            var btnColumn = new DataGridViewButtonColumn { UseColumnTextForButtonValue = true, Name = "Course Select", Text = "Select" };
            dgvCourses.Columns.Add(btnColumn);
            dgvCourses.DataSource = _facade.GetCourseTable();
        }

        #endregion
    }
}
